<?php
/**
 * Created by PhpStorm.
 * User: 刘红波 <liuhb@cqjcdflzxsws.com>
 * Date: 2018/11/20
 * Time: 15:09
 */


/**
 * Index a document
 *
 * Array
(
[_index] => my_index
[_type] => my_type
[_id] => my_id
[_version] => 1
[created] => 1
)
 */
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id',
    'body' => ['testField' => 'abc']
];

$response = $client->index($params);
print_r($response);

/**
 * Get a document
 * Array
            (
            [_index] => my_index
            [_type] => my_type
            [_id] => my_id
            [_version] => 1
            [found] => 1
            [_source] => Array
            (
            [testField] => abc
            )

            )
 *
 *
 *
 *
 */

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id'
];

$response = $client->get($params);
print_r($response);

/**
 * _search_for_a_document
 *
 * Array
(
[took] => 1
[timed_out] =>
[_shards] => Array
(
[total] => 5
[successful] => 5
[failed] => 0
)

[hits] => Array
(
[total] => 1
[max_score] => 0.30685282
[hits] => Array
(
[0] => Array
(
[_index] => my_index
[_type] => my_type
[_id] => my_id
[_score] => 0.30685282
[_source] => Array
(
[testField] => abc
)
)
)
)
)
 *
 */


$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'match' => [
                'testField' => 'abc'
            ]
        ]
    ]
];

$response = $client->search($params);
print_r($response);


/**
 * Delete a document
 *
 *
 *
 * Array
(
[found] => 1
[_index] => my_index
[_type] => my_type
[_id] => my_id
[_version] => 2
)
 *
 */

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id'
];

$response = $client->delete($params);
print_r($response);

/**
 *Delete an index
 *
 * Array
(
[acknowledged] => 1
)
 */


$deleteParams = [
    'index' => 'my_index'
];
$response = $client->indices()->delete($deleteParams);
print_r($response);


/**
 * Create an index
 *
 * Array
(
[acknowledged] => 1
)
 */
$params = [
    'index' => 'my_index',
    'body' => [
        'settings' => [
            'number_of_shards' => 2,
            'number_of_replicas' => 0
        ]
    ]
];

$response = $client->indices()->create($params);
print_r($response);